package com.cloudkinto.controller.orderback;


import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.service.orderback.vo.FbaOrderBackAddReq;
import com.cloudkinto.service.orderback.vo.FbaOrderBackDetailRes;
import com.cloudkinto.service.orderback.vo.FbaOrderBackPageRes;
import com.cloudkinto.service.orderback.vo.FbaOrderBackUpdateReq;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.cloudkinto.service.orderback.FbaOrderBackService;

import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.Map;

import com.cloudkinto.base.BaseController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ding
 * @since 2023-08-02
 */
@RestController
@Api(tags = "亚马逊退货入库模块")
@RequestMapping("/api/admin/fba/back")
public class FbaOrderBackController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(FbaOrderBackController.class);

    @Autowired
    private FbaOrderBackService service;

    @UserSessionRequired
    @GetMapping("/addInit")
    @ApiOperation(value = "新增初始化", notes = "新增初始化")
    public SingleResult addInit() {
        return service.addInit();
    }

    @UserSessionRequired
    @PostMapping("")
    @ApiOperation(value = "新增", notes = "新增")
    public SingleResult add(@RequestBody @Validated FbaOrderBackAddReq req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return SingleResult.success("新增成功", service.add(req, userId));
    }

    @UserSessionRequired
    @GetMapping("/updateInit/{id}")
    @ApiOperation(value = "编辑初始化", notes = "编辑初始化")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = FbaOrderBackDetailRes.class))
    public SingleResult updateInit(@PathVariable("id") Long id) {
        return service.updateInit(id);
    }

    @UserSessionRequired
    @PutMapping("")
    @ApiOperation(value = "修改", notes = "修改")
    public SingleResult update(@RequestBody @Validated FbaOrderBackUpdateReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功", service.update(req, userId));
    }

    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功", service.delete(id, userId));
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = FbaOrderBackDetailRes.class))
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", service.detail(id));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit();
    }
    @UserSessionRequired
    @GetMapping("/list")
    @ApiOperation(value = "查询", notes = "查询")
    public SingleResult list(HttpServletRequest request) {
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        map.put("keyword", keyword);
        return SingleResult.success("查询成功", service.getList(map));
    }

    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "keyword", value = "关键字搜索(追踪号/订单号/移除订单ID)", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "current", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "pageSize", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "companyId", value = "公司id", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "dateType", value = "时间类型(1 创建时间 2到货时间)", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "开始时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "订单状态", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "storageId", value = "所属仓库ID", dataType = "long", paramType = "query")

    })

    @UserSessionRequired
    @ApiResponses(@ApiResponse(code = 200, message = "", response = FbaOrderBackPageRes.class))
    public PageResult listPage(HttpServletRequest request) {
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        String storageId = request.getParameter("storageId");
        String companyIdC = request.getParameter("companyId");
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        String dateType = request.getParameter("dateType");
        String status = request.getParameter("status");
        map.put("keyword", keyword);
        map.put("current", current);
        map.put("pageSize", size);
        map.put("status", status);
        map.put("storageId", storageId);
        map.put("dateType", dateType);
        if (companyId != null) {
            map.put("companyId", companyId + "");
        }
        if (StringUtils.isNotBlank(companyIdC)) {
            map.put("companyId", companyIdC);
        }
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        return service.getListPage(map);
    }

}

